package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

/**
 * @author Fengkai
 * @creat 2023/4/12
 * 重复代码查找方法:以字符串形式给出两行代码(字符审长度1< length < 100，由英文字母、数字和空格组成)，找出两行代码中的最长公共子串
 * 注:如果不存在公共子串，返回空字符串
 * 输入描述
 * 输入的参数 text1，text2 分别表示两行代码
 * 输出描述
 * 输出任一最长公共子串
 * 示例一
 * 输入
 * hello123world1
 * hello123abc4
 * 输出
 * hello123
 */
public class FindRepeatCode {

    public static void main (String[] args) {
        System.out.println(getRepeatCode("hiworld", "hiweb"));

    }

    public static String getRepeatCode (String code1, String code2) {
        String result = "";
        if (code1.length() > code2.length()) {
            String cache = code1;
            code1 = code2;
            code2 = cache;
        }
        int maxlength = 0;
        String out = "";
        out:for (int i = 0; i < code1.length(); i++) {
            for (int j = i + 1; j <= code1.length(); j++) {
                String tmp = code1.substring(i, j);
                if (code2.contains(tmp) && tmp.length() >= out.length()){
                    maxlength = tmp.length();
                    out = tmp;
                }else {
                    continue out;
                }
            }
        }
        if (maxlength == 1) {
            return "";
        }
        return out;
    }
}
